package yz.cp.back.s.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yz.cp.back.s.entity.SysDepartment;
import yz.cp.back.s.entity.SysPermission;
import yz.cp.back.s.entity.SysRole;
import yz.cp.back.s.entity.SysUser;
import yz.cp.back.s.mapper.SysUserMapper;
import yz.cp.back.s.service.ISysDepartmentService;
import yz.cp.back.s.service.ISysPermissionService;
import yz.cp.back.s.service.ISysUserRoleService;
import yz.cp.back.s.service.ISysUserService;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-04-03
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    private ISysDepartmentService departmentService;
    @Autowired
    private ISysUserRoleService userRoleService;
    @Autowired
    private ISysPermissionService permissionService;
    @Override
    public SysUser findByUsername(String username) {
        List<SysUser> list=list(new QueryWrapper<SysUser>().lambda().eq(SysUser::getUsername, username));
        if(list!=null&&list.size()>0){
            SysUser user = list.get(0);
            // 关联部门
            if(StrUtil.isNotBlank(user.getDepartmentId())){
                SysDepartment department = departmentService.getById(user.getDepartmentId());
                user.setDepartmentTitle(department.getTitle());
            }
            // 关联角色
            List<SysRole> roleList = userRoleService.findByUserId(user.getId());
            user.setRoles(roleList);
            // 关联权限菜单
            List<SysPermission> permissionList =  permissionService.findByUserId(user.getId());
            user.setPermissions(permissionList);
            return user;
        }
        return null;
    }


    @Override
    public SysUser findByMobile(String mobile) {
        List<SysUser> list =list(new QueryWrapper<SysUser>().lambda().eq(SysUser::getMobile,mobile));
        if(list!=null&&list.size()>0) {
            SysUser user = list.get(0);
            return user;
        }
        return null;
    }

    @Override
    public SysUser findByEmail(String email) {
        List<SysUser> list =list(new QueryWrapper<SysUser>().lambda().eq(SysUser::getEmail,email));
        if(list!=null&&list.size()>0) {
            SysUser user = list.get(0);
            return user;
        }
        return null;
    }



    @Override
    public List<SysUser> findByDepartmentId(String departmentId) {
        return list(new QueryWrapper<SysUser>().lambda().eq(SysUser::getDepartmentId,departmentId));
    }

    @Override
    public List<SysUser> findByUsernameLikeAndStatus(String username, Integer status) {
        return list(new QueryWrapper<SysUser>().lambda().eq(SysUser::getUsername,username)
                .eq(SysUser::getStatus,status));
    }





}
